from typing import Optional
from pydantic import BaseModel, ConfigDict, field_validator
from uuid import UUID


class EvaluationBase(BaseModel):
    activity_id: UUID
    version_no: int = 1
    score_total: float = 0.0
    verdict: bool = False
    level: Optional[str] = None
    summary: Optional[str] = None
    template_version_code: Optional[str] = None

    @field_validator('verdict', mode='before')
    @classmethod
    def _parse_verdict(cls, v):
        if isinstance(v, bool):
            return v
        s = str(v).strip().lower()
        if s in ('veto', 'true', '1', 'yes', 'y', 'on'):
            return True
        if s in ('qualified', 'unqualified', 'false', '0', 'no', 'n', 'off', ''):
            return False
        return bool(v)


class EvaluationCreate(EvaluationBase):
    pass


class EvaluationRead(EvaluationBase):
    id: UUID

    model_config = ConfigDict(from_attributes=True, json_encoders={
        UUID: str
    })


class EvaluationItemScoreBase(BaseModel):
    evaluation_id: UUID
    code: str
    score_value: Optional[float] = None
    is_veto: bool = False
    comment: Optional[str] = None


class EvaluationItemScoreCreate(EvaluationItemScoreBase):
    pass


class EvaluationItemScoreRead(EvaluationItemScoreBase):
    id: UUID

    model_config = ConfigDict(from_attributes=True, json_encoders={
        UUID: str
    })